//
//  IcRecommendationDetailViewController.m
//  IceClear
//
//  Created by P & C on 3/22/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "IcRecommendationDetailViewController.h"
#import "IcMaterial.h"
#import "IcMaterialDetailViewController.h"
#import "IcRecommendationsListViewController.h"


@implementation IcRecommendationDetailViewController

@synthesize recommendation;
@synthesize appModelsManager;


-(void)onSaveRecommendationAction:(id)sender {
	NSLog(@"Save Recommendation");
	
	// save the recommendation
	// .... probably use a UIAlertView to get
	// a name to remember it by
	
		
	UIAlertView *prompt = [[UIAlertView alloc] initWithTitle:@"Save Recommendation as Profile" 
													 message:@"\n\n" // IMPORTANT
													delegate:self 
										   cancelButtonTitle:@"Cancel" 
										   otherButtonTitles:@"Enter", nil];
	
	textField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 80.0, 260.0, 25.0)]; 
	[textField setBackgroundColor:[UIColor whiteColor]];
	[textField setPlaceholder:@"Enter Profile Name "];	
	[textField becomeFirstResponder];
	[prompt addSubview:textField];	
	
	// set place
	
	[prompt show];
    [prompt release];
	
	}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
	NSLog(@"Button index %d", buttonIndex);
	if (buttonIndex == 0)
	{
		NSLog(@"Hit Cancel" );
	}
	else if ((buttonIndex == 1) && ([textField.text length]>0))
	{
		NSLog(@"Hit Enter %d",[textField.text length]);
		recommendation.name = textField.text;
		[self.appModelsManager.recommendationsManager saveNewRecommendation:recommendation];
		

		
	}else{
		UIAlertView *prompt1 = [[UIAlertView alloc] initWithTitle:@"Save Recommendation as Profile" 
														 message:@"\n\n" // IMPORTANT
														delegate:self 
											   cancelButtonTitle:@"Cancel" 
											   otherButtonTitles:@"Enter", nil];
		
		textField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 80.0, 260.0, 25.0)]; 
		[textField setBackgroundColor:[UIColor whiteColor]];
		[textField setPlaceholder:@"Enter Profile Name "];
		[textField becomeFirstResponder];
		[prompt1 addSubview:textField];	
		
		
		// set place
		[prompt1 show];
		[prompt1 release];
	
	}
}


#pragma mark -
#pragma mark Initialization

/*
- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/


#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];

    if (self.navigationController && self.recommendation.name.length == 0) {
		// initialize navigation controls
		
		// initialize navigation controls
		
		UIBarButtonItem* btnSave = [[[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStyleBordered 
																	target:self action:@selector(onSaveRecommendationAction:)] 
									autorelease];
		
		self.navigationItem.rightBarButtonItem = btnSave;
	}
	
	self.view.backgroundColor = [UIColor clearColor];
}

/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    int num = 0;
	
	switch (section) {
		case 0:
			num = 4;
			break;
		case 1:
			num = [recommendation.materials count];
			if (num == 0) {
				num = 1;
			}
			break;
		default:
			break;
	}
	
	return num;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Configure the cell...
    NSString* text = @"Nothing yet...";
	
	if (indexPath.section == 0) {
		NSString* afterBefore = recommendation.usageType == 0 ? @"Before" : @"After";
		
		switch (indexPath.row) {
			case 0:
				text = [NSString stringWithFormat:@"To: %@",
						[appModelsManager.recommendationsManager usageOfType:recommendation.usageType]];
				break;
			case 1:
				text = [NSString stringWithFormat:@"A: %@",
						[appModelsManager.recommendationsManager facilityOfType:recommendation.facilityType]];
				break;
			case 2:
				text = [NSString stringWithFormat:@"At: %d Degs", recommendation.temperature];
				break;
			case 3:
				text = [NSString stringWithFormat:@"%@: %@", afterBefore,
						[appModelsManager.recommendationsManager precipitationOfType:recommendation.precipitationType]];
				break;
			default:
				break;
		}
	}
	else if (indexPath.section == 1) {
		if ([recommendation.materials count] > 0) {
			text = [(IcMaterial *)[recommendation.materials objectAtIndex:indexPath.row] title];
		}
		else {
			text = @"Oops! This is embarrassing. We are sorry but we are stumped on this one.";
		}
		
		cell.textLabel.numberOfLines = 0;
		cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
		cell.textLabel.font = [UIFont systemFontOfSize:15];
	}
	
	cell.textLabel.text = text;
	
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	NSString* t = @"";
	
	switch (section) {
		case 0:
			t = @"If you would like";
			break;
		case 1:
			t = @"We recommend you use";
			break;
		default:
			break;
	}
	
	return t;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	if (indexPath.section == 1) {
		// display details about the selected recommended material
		
		IcMaterialDetailViewController *detailViewController = [[IcMaterialDetailViewController alloc] 
																initWithStyle:UITableViewStyleGrouped];
		IcMaterial* material = (IcMaterial *)[self.recommendation.materials objectAtIndex:indexPath.row];
		
		detailViewController.appModelsManager = self.appModelsManager;
		detailViewController.title = material.title;
		detailViewController.material = material;
		
		// Pass the selected object to the new view controller.
		[self.navigationController pushViewController:detailViewController animated:YES];
		[detailViewController release];
	}
}

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	if (indexPath.section == 0) {
		return nil;
	}
	else {
		return indexPath;
	}

}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
	[recommendation release];
	[appModelsManager release];
    [super dealloc];
}


@end

